<template>
    <div class="register-container">
        <el-form :model="registerForm" :rules="rules" ref="registerForm" class="register-form">
            <h3 class="title">用户注册</h3>
            <el-form-item prop="account">
                <el-input v-model="registerForm.account" placeholder="请输入账号">
                    <template slot="prepend">
                        <i class="el-icon-user"></i>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="registerForm.password" type="password" placeholder="请输入密码">
                    <template slot="prepend">
                        <i class="el-icon-lock"></i>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item prop="confirmPassword">
                <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请确认密码">
                    <template slot="prepend">
                        <i class="el-icon-lock"></i>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item prop="email">
                <el-input v-model="registerForm.email" placeholder="请输入邮箱">
                    <template slot="prepend">
                        <i class="el-icon-message"></i>
                    </template>
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" style="width:100%;" @click="handleRegister">注册</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="text" @click="$router.push('/login')">已有账号？立即登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: 'Register',
    data() {
        const validatePass2 = (rule, value, callback) => {
            if (value !== this.registerForm.password) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            registerForm: {
                account: '',
                password: '',
                confirmPassword: '',
                email: ''
            },
            rules: {
                account: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
                ],
                confirmPassword: [
                    { required: true, message: '请再次输入密码', trigger: 'blur' },
                    { validator: validatePass2, trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        async handleRegister() {
            try {
                await this.$refs.registerForm.validate();
                const response = await this.$axios({
                    method: 'post',
                    url: '/car/register',
                    data: {
                        account: this.registerForm.account,
                        password: this.registerForm.password,
                        email: this.registerForm.email
                    }
                });
                
                if (response.data.code === 0) {
                    this.$message.success('注册成功');
                    this.$router.push('/login');
                } else {
                    this.$message.error(response.data.msg || '注册失败');
                }
            } catch (error) {
                console.error('注册失败:', error);
                this.$message.error('注册失败，请重试');
            }
        }
    }
}
</script>

<style scoped>
.register-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f3f3f3;
}

.register-form {
    width: 350px;
    padding: 35px;
    border-radius: 5px;
    background: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
}

.title {
    text-align: center;
    margin: 0 0 30px;
    color: #303133;
}
</style>